//工具函数库，仔仔编写。

let _qdcat = {


	async GetString(str, data) {

		let searchArr = [];
		let arr = data.split("$");
		let firstStr = arr[0]
		let secondStr = arr[1]


		if (str == "" || str == null || str == undefined) { // "",null,undefined
			return "";
		}
		if (str.indexOf(firstStr) < 0) {
			return "";
		}
		if (str) {
			var subFirstStr = str.substring(str.indexOf(firstStr) + firstStr.length, str.length);
			var subSecondStr = subFirstStr.substring(0, subFirstStr.indexOf(secondStr));
		}

		let liArr = subSecondStr.split("</li>")
		//let xiao=subSecondStr.split("<i></i>") 

		for (let i = 0; i < liArr.length; i++) {
			let catname = await this.GetInterceptTheSpecifiedString(liArr[i], '<h3 class="module-title">$</h3>')
			let sum = await this.GetInterceptTheSpecifiedString(liArr[i], 'output>$</output>')
			let catlist = await this.GetInterceptTheSpecifiedString(liArr[i],
				'<div class="sort-li-detail">$</div>')
			let catArr = catlist.split("</a>")

			let chArr = []
			for (let s = 0; s < catArr.length; s++) {


				let name = await this.GetInterceptTheSpecifiedString(catArr[s], 'data-tid="$"')
				let url = await this.GetInterceptTheSpecifiedString(catArr[s], 'href="//$/"')
				let cid = this.getNum(url, 'catid', '/') ? this.getNum(url, 'catid', '/') : this.getNum(url,
					'chanId', '-');
				let pid = this.getNum(url, 'subcatid', '-') ? this.getNum(url, 'subcatid', '-') : this.getNum(
					url + '/', 'subCateId', '/');
				if (name) {
					let newJsonx = {
						listname: name,
						url: 'https://' + url,
						cid: cid,
						pid: pid
			
	}

					chArr.push(newJsonx);
				}

			}

			if (catname) {
				let newJson = {
					catname: catname,
					sum: sum,
					data: chArr
				}
				searchArr.push(newJson);
			}
		}

		return searchArr;
	},
	fun(arr) {
		let newsArr = [];
		for (let i = 0; i < arr.length; i++) {
			if (newsArr.indexOf(arr[i]) === -1) {
				newsArr.push(arr[i]);
			}
		}
		return newsArr;
	},
	getNum(str, firstStr, secondStr) {
		if (str == "" || str == null || str == undefined) { // "",null,undefined
			return "";
		}
		if (str.indexOf(firstStr) < 0) {
			return "";
		}
		var subFirstStr = str.substring(str.indexOf(firstStr) + firstStr.length, str.length);
		var subSecondStr = subFirstStr.substring(0, subFirstStr.indexOf(secondStr));
		return subSecondStr;
	},
	//截取指定字符串
	async GetInterceptTheSpecifiedString(str, data) {
		let arr = data.split("$");
		let firstStr = arr[0]
		let secondStr = arr[1]


		if (str == "" || str == null || str == undefined) { // "",null,undefined
			return "";
		}
		if (str.indexOf(firstStr) < 0) {
			return "";
		}
		if (str) {
			var subFirstStr = str.substring(str.indexOf(firstStr) + firstStr.length, str.length);
			var subSecondStr = subFirstStr.substring(0, subFirstStr.indexOf(secondStr));
		}
		return subSecondStr;
	},
	getmale() {

		return new Promise(function(resolve, reject) {
			uni.request({
				url: 'https://m.qidian.com/category/',
				withCredentials: true,
				header: {
					'content-type': 'application/json', //自定义请求头信息
					'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 wechatdevtools/1.06.2210310 MicroMessenger/8.0.5 Language/zh_CN webview/',
					'Host': 'm.qidian.com',
					'Referer': 'https://servicewechat.com/wx82057be8deb1aae2/devtools/page-frame.html',
					'Sec-Fetch-Dest': 'empty',
					'Sec-Fetch-Mode': 'cors',
					'Sec-Fetch-Site': 'cross-site'
				},
				success: function(re) {
					console.log(re.cookies)
					uni.setStorageSync('qd_cookies', re.cookies);
					resolve(re)
				}
			});

		});


	},
	getfemale() {

		return new Promise(function(resolve, reject) {
			uni.request({
				url: 'https://m.qdmm.com/all/',
				withCredentials: true,
				header: {
					'content-type': 'application/json', //自定义请求头信息
					'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 wechatdevtools/1.06.2210310 MicroMessenger/8.0.5 Language/zh_CN webview/',
					'Host': 'm.qdmm.com',
					'Referer': 'https://servicewechat.com/wx82057be8deb1aae2/devtools/page-frame.html',
					'Sec-Fetch-Dest': 'empty',
					'Sec-Fetch-Mode': 'cors',
					'Sec-Fetch-Site': 'cross-site'
				},
				success: function(re) {

					uni.setStorageSync('qd_cookies', re.cookies);
					resolve(re)
				}
			});

		});


	},
	categories(cid, limit, page, type) {

		let url = 'https://weread.qq.com/web/bookListInCategory/' + cid + '?maxIndex=' + (limit*page)
		console.log('-----',url)
		return new Promise(function(resolve, reject) {
			uni.request({
				url: url,
				timeout: 6000,
				method: 'GET',
				sslVerify: false,
				withCredentials: true,
		 	    success: (res) => {
					resolve(res.data.code ? [] : res.data.books);
				}
			});
		
		});
	}
};

export default _qdcat;
